import { Component } from "@angular/core";
import {
  NavController,
  NavParams,
  ToastController,
  AlertController
} from "ionic-angular";
import { ApplyRepairBean } from "./bean/apply-repair.bean";
import { HttpService } from "../../providers/http.service";
import * as moment from "moment";
import { Storage } from "@ionic/storage";
import { HomePage } from "./../home/home";
@Component({
  selector: "page-repair-apply",
  templateUrl: "apply-repair.html"
})
export class ApplyRepairPage {
  private applyRepairBean: ApplyRepairBean = new ApplyRepairBean();
  private title: string;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public httpService: HttpService,
    private storage: Storage,
    private toast: ToastController,
    private alertCtrl: AlertController
  ) {
    this.applyRepairBean.blocksNum = this.navParams.get("buildAddr");
    this.applyRepairBean.applyType = this.navParams.get("applyType");
    if (this.navParams.get("applyType") == "2") {
      this.title = '召';
    } else {
      this.title = '报';
    }
    storage.get("user").then(val => {
      this.applyRepairBean.partyName = val.partyName;
      this.applyRepairBean.recorderName = val.userName;
      this.applyRepairBean.sysUserId = val.id;
      this.applyRepairBean.roleId = val.roleId;
    });
  }

  send() {
    if (this.navParams.get("applyType") == "1" && !this.applyRepairBean.cost) {
      const alert = this.alertCtrl.create({
        // title: '',
        subTitle: "预估费用必填",
        buttons: ["确定"]
      });
      alert.present();
      return;
    }
    if (!this.applyRepairBean.applyQuestion) {
      const alert = this.alertCtrl.create({
        // title: '',
        subTitle: "请说明您的问题",
        buttons: ["确定"]
      });
      alert.present();
      return;
    }
    if (!this.applyRepairBean.repairTime) {
      const alert = this.alertCtrl.create({
        // title: '',
        subTitle: "请选择您想维修的时间",
        buttons: ["确定"]
      });
      alert.present();
      return;
    } else {
      if (moment(this.applyRepairBean.repairTime).isBefore(new Date())) {
        const alert = this.alertCtrl.create({
          // title: '',
          subTitle: "维修时间不能早于现在",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }

    if (this.applyRepairBean.applyType == "2") {
      if (!this.applyRepairBean.usedUserUrl) {
        const alert = this.alertCtrl.create({
          subTitle: "请签名",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }
    if (this.applyRepairBean.applyType == "1") {
      if (!this.applyRepairBean.maintenanceUsedUrl) {
        const alert = this.alertCtrl.create({
          subTitle: "请签名",
          buttons: ["确定"]
        });
        alert.present();
        return;
      }
    }

    this.applyRepairBean.applyTime = moment(new Date()).format(
      "YYYYMMDDHHmmss"
    );
    this.applyRepairBean.elevatorId = this.navParams.get("elevatorId");

    this.applyRepairBean.repairTime = this.applyRepairBean.repairTime
      .replace(/-/g, "")
      .replace(/T/g, "")
      .replace(/Z/g, "")
      .replace(/:/g, "");

    this.httpService.addAppliyRepair(this.applyRepairBean).subscribe(data => {
      if (data.status == 200) {
        let tt = this.toast.create({
          message: "提交成功",
          duration: 2000,
          position: "top"
        });
        tt.present().then(() => {
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            this.navCtrl
              .getActive()
              .getNav()
              .remove(0);
          });
        });
      } else {
        const alert = this.alertCtrl.create({
          // title: '',
          subTitle: "报修失败",
          buttons: ["确定"]
        });
        alert.present();
      }
    });
  }

  getImgUrl(param) {
    if (this.navParams.get("applyType") == "2") {
      this.applyRepairBean.usedUserUrl = param.imageDate;
    }
    if (this.navParams.get("applyType") == "1") {
      this.applyRepairBean.maintenanceUsedUrl = param.imageDate;
    }
  }
}
